/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ArtistaListar.java
 *
 * Created on 28/10/2011, 21:35:08
 */
package br.com.beethoven.ui;

import br.com.beethoven.exception.BDException;
import br.com.beethoven.model.entidade.Banda;
import br.com.beethoven.model.entidade.vo.BandaVO;
import br.com.beethoven.model.fachada.IGerenciarBandaFacade;
import br.com.beethoven.model.fachada.impl.GerenciarBandaFacade;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author gustavoalves
 */
public class ArtistaListar extends javax.swing.JFrame {
    private IGerenciarBandaFacade fachadaBanda;
    /** Creates new form ArtistaListar */
    public ArtistaListar() {
        initComponents();
        
        this.listarBandas();
    }
    public void listarBandas(){
        fachadaBanda = new GerenciarBandaFacade();
        List<BandaVO> listaBandas;
        try {
            listaBandas = fachadaBanda.listar();
            Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();
        tableHeaders.add("Nome");

        for(int i=0;i<listaBandas.size();i++) {
            Vector<Object> oneRow = new Vector<Object>();
            boolean add = oneRow.add(listaBandas.get(i));
            tableData.add(oneRow);
        }
        tabelaArtistas.setModel(new DefaultTableModel(tableData, tableHeaders));
        } catch (BDException ex) {
            Logger.getLogger(ArtistaListar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaArtistas = new javax.swing.JTable();
        botaoEditarArtista = new javax.swing.JButton();
        botaoExluirArtista = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        artistaMenu = new javax.swing.JMenu();
        artistaCadastrarMenu = new javax.swing.JMenuItem();
        artistaListarMenu = new javax.swing.JMenuItem();
        albumMenu = new javax.swing.JMenu();
        albumCadastrarMenu = new javax.swing.JMenuItem();
        albumListarMenu = new javax.swing.JMenuItem();
        musicaMenu = new javax.swing.JMenu();
        musicaCadastrarMenu = new javax.swing.JMenuItem();
        musicaListarMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabelaArtistas.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tabelaArtistas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome"
            }
        ));
        jScrollPane1.setViewportView(tabelaArtistas);

        botaoEditarArtista.setText("Editar");
        botaoEditarArtista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoEditarArtistaActionPerformed(evt);
            }
        });

        botaoExluirArtista.setText("Excluir");
        botaoExluirArtista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExluirArtistaActionPerformed(evt);
            }
        });

        jToggleButton1.setText("Detalhar");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        artistaMenu.setText("Artista");

        artistaCadastrarMenu.setText("Cadastrar");
        artistaCadastrarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                artistaCadastrarMenuActionPerformed(evt);
            }
        });
        artistaMenu.add(artistaCadastrarMenu);

        artistaListarMenu.setText("Listar");
        artistaListarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                artistaListarMenuActionPerformed(evt);
            }
        });
        artistaMenu.add(artistaListarMenu);

        jMenuBar1.add(artistaMenu);

        albumMenu.setText("Álbum");

        albumCadastrarMenu.setText("Cadastrar");
        albumCadastrarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                albumCadastrarMenuActionPerformed(evt);
            }
        });
        albumMenu.add(albumCadastrarMenu);

        albumListarMenu.setText("Listar");
        albumListarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                albumListarMenuActionPerformed(evt);
            }
        });
        albumMenu.add(albumListarMenu);

        jMenuBar1.add(albumMenu);

        musicaMenu.setText("Música");

        musicaCadastrarMenu.setText("Cadastrar");
        musicaCadastrarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                musicaCadastrarMenuActionPerformed(evt);
            }
        });
        musicaMenu.add(musicaCadastrarMenu);

        musicaListarMenu.setText("Listar");
        musicaListarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                musicaListarMenuActionPerformed(evt);
            }
        });
        musicaMenu.add(musicaListarMenu);

        jMenuBar1.add(musicaMenu);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(40, 40, 40)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 439, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(botaoExluirArtista, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .add(botaoEditarArtista, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .add(jToggleButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jToggleButton1)
                        .add(19, 19, 19)
                        .add(botaoEditarArtista)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(botaoExluirArtista))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 269, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void artistaCadastrarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_artistaCadastrarMenuActionPerformed
// TODO add your handling code here:
    ArtistaCadastro artista = new ArtistaCadastro();
    artista.setVisible(true);
    this.dispose();
}//GEN-LAST:event_artistaCadastrarMenuActionPerformed

private void artistaListarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_artistaListarMenuActionPerformed
// TODO add your handling code here:
    ArtistaListar artista = new ArtistaListar();
            artista.setVisible(true);
       this.dispose();
    
}//GEN-LAST:event_artistaListarMenuActionPerformed

private void albumCadastrarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_albumCadastrarMenuActionPerformed
// TODO add your handling code here:
    CdCadastro cd = new CdCadastro();
    cd.setVisible(true);
    this.dispose();
}//GEN-LAST:event_albumCadastrarMenuActionPerformed

private void albumListarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_albumListarMenuActionPerformed
// TODO add your handling code here:
    CdListar cd = new CdListar();
    cd.setVisible(true);
    this.dispose();
}//GEN-LAST:event_albumListarMenuActionPerformed

private void musicaCadastrarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_musicaCadastrarMenuActionPerformed
// TODO add your handling code here:
    MusicaCadastro musica;
        try {
            musica = new MusicaCadastro();
            musica.setVisible(true);
            this.dispose();
        } catch (BDException ex) {
            Logger.getLogger(PaginaInicial.class.getName()).log(Level.SEVERE, null, ex);
        }
    
}//GEN-LAST:event_musicaCadastrarMenuActionPerformed

private void musicaListarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_musicaListarMenuActionPerformed
// TODO add your handling code here:
    MusicaListar musica = new MusicaListar();
    musica.setVisible(true);
    this.dispose();
}//GEN-LAST:event_musicaListarMenuActionPerformed

private void botaoExluirArtistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExluirArtistaActionPerformed
    // TODO add your handling code here:
    int linhaSelecionada = tabelaArtistas.getSelectedRow();
        try {
            if(linhaSelecionada == -1)
                JOptionPane.showMessageDialog(this, "Nenhuma Banda Selecionada");
            else{
                if(JOptionPane.showConfirmDialog(this, "Deseja realmente excluir a Banda?")==0){
                    fachadaBanda.remover(fachadaBanda.listar().get(linhaSelecionada));
                    this.listarBandas();
                }
            }
        } catch (BDException ex) {
            JOptionPane.showMessageDialog(this, "Não foi possivel excluir.");
            Logger.getLogger(ArtistaListar.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception e){
            JOptionPane.showMessageDialog(this, "Não foi possivel excluir, pois a banda está em uso.");
        }
}//GEN-LAST:event_botaoExluirArtistaActionPerformed

private void botaoEditarArtistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoEditarArtistaActionPerformed
    // TODO add your handling code here:
    int linhaSelecionada = tabelaArtistas.getSelectedRow();
    try {
            if(linhaSelecionada == -1)
                JOptionPane.showMessageDialog(this, "Nenhuma Banda Selecionada");
            else{
                this.preencherEditarArtista(fachadaBanda.listar().get(linhaSelecionada));
            }
        } catch (BDException ex) {
            JOptionPane.showMessageDialog(this, "Não foi possivel excluir.");
            Logger.getLogger(ArtistaListar.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception e){
            JOptionPane.showMessageDialog(this, "Não foi possivel excluir, pois a banda está em uso.");
        }
}//GEN-LAST:event_botaoEditarArtistaActionPerformed

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
    // TODO add your handling code here:
    int linhaSelecionada = tabelaArtistas.getSelectedRow();
    try {
            if(linhaSelecionada == -1)
                JOptionPane.showMessageDialog(this, "Nenhuma Banda Selecionada");
            else{
                this.preencherDetalharArtista(fachadaBanda.listar().get(linhaSelecionada));
            }
        } catch (BDException ex) {
            JOptionPane.showMessageDialog(this, "Não foi possivel detalhar.");
            Logger.getLogger(ArtistaListar.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception e){
            JOptionPane.showMessageDialog(this, "Não foi possivel detalhar.");
        }
}//GEN-LAST:event_jToggleButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ArtistaListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ArtistaListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ArtistaListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ArtistaListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
             
                    new ArtistaListar().setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem albumCadastrarMenu;
    private javax.swing.JMenuItem albumListarMenu;
    private javax.swing.JMenu albumMenu;
    private javax.swing.JMenuItem artistaCadastrarMenu;
    private javax.swing.JMenuItem artistaListarMenu;
    private javax.swing.JMenu artistaMenu;
    private javax.swing.JButton botaoEditarArtista;
    private javax.swing.JButton botaoExluirArtista;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JMenuItem musicaCadastrarMenu;
    private javax.swing.JMenuItem musicaListarMenu;
    private javax.swing.JMenu musicaMenu;
    private javax.swing.JTable tabelaArtistas;
    // End of variables declaration//GEN-END:variables

    private void preencherEditarArtista(BandaVO _bandaVO) {
        EditarArtista editarArtista = new EditarArtista();
        editarArtista.preencherEditarArtistaEditar(_bandaVO);
        editarArtista.setVisible(true);
        this.dispose();
    }

    private void preencherDetalharArtista(BandaVO _bandaVO) {
        ArtistaDetalhar artistaDetalhar = new ArtistaDetalhar();
        artistaDetalhar.preencherDetalharArtista(_bandaVO);
        artistaDetalhar.setVisible(true);

    }
}
